package com.healthtop.service.impl;

import com.healthtop.dao.XsRoleMenuDao;
import com.healthtop.dto.RoleMenuDto;
import com.healthtop.model.XsRoleMenu;
import com.healthtop.service.XsRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class XsRoleMenuServiceImpl implements XsRoleMenuService {

    @Autowired
    private XsRoleMenuDao xsRoleMenuDao;

    @Override
    public List<XsRoleMenu> selectByRoleId(long roleId) {
        return xsRoleMenuDao.selectByRoleId(roleId);
    }

    @Override
    public void deleteByRoleId(Long roleId) {
        xsRoleMenuDao.delete(roleId);
    }

    @Override
    public void submitRoleMenu(RoleMenuDto roleMenuDto) {
        if(roleMenuDto.getRoleId()!=0) {
            xsRoleMenuDao.delete(roleMenuDto.getRoleId());
            if (!StringUtils.isEmpty(roleMenuDto.getMenuIds())) {
                String[] menuIds = roleMenuDto.getMenuIds().split(",");
                for (int i = 0; i < menuIds.length; i++) {
                    XsRoleMenu xsRoleMenu = new XsRoleMenu();
                    xsRoleMenu.setMenuId(Long.parseLong(menuIds[i]));
                    xsRoleMenu.setRoleId(roleMenuDto.getRoleId());
                    xsRoleMenuDao.insert(xsRoleMenu);
                }
            }
        }
    }
}
